package com.haixiaoke.saas.common.utils.sms;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.haixiaoke.saas.common.config.sms.SmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 模板短信工具
 *
 * @author xiaolong
 * @createDate: 2024/5/9
 * @version: 1.0
 */
@Slf4j
@Component
public class SmsUtils {


    @Autowired
    private SmsConfig smsConfig;


    public boolean sendTemplateSmsWithData(String mobile, String templateId, String[] datas) {
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(smsConfig.getServerIp(), smsConfig.getServerPort());
        sdk.setAccount(smsConfig.getAccountSid(), smsConfig.getAccountToken());
        sdk.setAppId(smsConfig.getAppid());
        sdk.setBodyType(BodyType.Type_JSON);

        HashMap<String, Object> result = sdk.sendTemplateSMS(mobile, templateId, datas, null, null);
        if ("000000".equals(result.get("statusCode"))) {
            log.info("SmsService sendTemplateSmsWithData success, statusCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return true;
        } else {
            log.info("SmsService sendTemplateSmsWithData fail, errorCode=" + result.get("statusCode") + " msg= "
                    + result.get("statusMsg"));
            return false;
        }
    }


}
